/***********************************************************************
    filename:   ComponentBase.cpp
    created:    Mon Jul 18 2005
    author:     Paul D Turner <paul@cegui.org.uk>
*************************************************************************/

#include "renderers/ComponentBase.h"
#include "hgeExceptions.h"
#include "hgePropertyHelper.h"
#include "hgecolor.h"
#include <iostream>

ComponentBase::ComponentBase() :
    d_colours(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),
    d_colourProperyIsRect(false)
{
}

ComponentBase::~ComponentBase()
{}

void ComponentBase::render(Window& srcWindow, const ColourRect* modColours, const hgeRect* clipper, bool clipToDisplay) const
{
    hgeRect destRect(Point(0, 0), srcWindow.getPixelSize());
    render_impl(srcWindow, destRect, modColours, clipper, clipToDisplay);
}

void ComponentBase::render(Window& srcWindow, const hgeRect& baseRect, const ColourRect* modColours, const hgeRect* clipper, bool clipToDisplay) const
{
	hgeRect destRect(Point(0, 0), srcWindow.getPixelSize());
    render_impl(srcWindow, destRect, modColours, clipper, clipToDisplay);
}

const hgeRect& ComponentBase::getComponentArea() const
{
    return d_area;
}

void ComponentBase::setComponentArea(const hgeRect& area)
{
    d_area = area;
}

const ColourRect& ComponentBase::getColours() const
{
    return d_colours;
}

void ComponentBase::setColours(const ColourRect& cols)
{
    d_colours = cols;
}

void ComponentBase::setColoursPropertySource(const String& property)
{
    d_colourPropertyName = property;
}

void ComponentBase::setColoursPropertyIsColourRect(bool setting)
{
    d_colourProperyIsRect = setting;
}

void ComponentBase::initColoursRect(const Window& wnd, const ColourRect* modCols, ColourRect& cr) const
{
    // if colours come via a colour property
    if (!d_colourPropertyName.empty())
    {
        // if property accesses a ColourRect
        if (d_colourProperyIsRect)
        {
            cr = PropertyHelper::stringToColourRect(wnd.getProperty(d_colourPropertyName));
        }
        // property accesses a colour
        else
        {
            colour val(PropertyHelper::stringToColour(wnd.getProperty(d_colourPropertyName)));
            cr.d_top_left     = val;
            cr.d_top_right    = val;
            cr.d_bottom_left  = val;
            cr.d_bottom_right = val;
        }
    }
    // use explicit ColourRect.
    else
    {
        cr = d_colours;
    }

    if (modCols)
    {
        cr *= *modCols;
    }
}


void ComponentBase::setVertFormattingPropertySource(const String& property)
{
    d_vertFormatPropertyName = property;
}

void ComponentBase::setHorzFormattingPropertySource(const String& property)
{
    d_horzFormatPropertyName = property;
}

